<?php

namespace app\beautiful\controller\admin;

use app\BaseController;
use app\common\model\SupplierGoodsModel;
use app\common\model\SupplierModel;


class Supplier extends BaseController{

    public function list(){
        $limit=$this->request->limit;
        $page=$this->request->page;
        $where=[];
        $shop_id=$this->request->shopId;
        $where['shop_id']=$shop_id;
        $order['a.id']='desc';
        $res_data=SupplierModel::list($where,$order,$page,$limit);
        return $this->success($res_data);
    }
    public function info(){
        $id = $this->request->param('id');
        if(empty($id)){
            return $this->error('ID错误');
        }
        $where['id']=$id;
        $res_data=SupplierModel::info($where);
        return $this->success($res_data);
    }
    public function effect(){
        $id = $this->request->param('id');
        if(empty($id)){
            return $this->error('ID错误');
        }
        $is_effect=$this->request->param('is_effect',null);
        if($is_effect===null){
            return $this->error('is_effect参数错误');
        }
        $where['id']=$id;
        $up['is_effect']=$is_effect;
        $res=SupplierModel::up($up,$where);
        return $this->success();
    }
    public function del(){
        $id = $this->request->param('id');
        if(empty($id)){
            return $this->error('ID错误');
        }
        $is_delete=1;
        $where['id']=$id;
        $info=SupplierModel::info($where);
        if(empty($info)){
            return $this->error('数据不存在');
        }
        $up['is_delete']=$is_delete;
        $res=SupplierModel::up($up,$where);
        if(!$res){
            return $this->error('删除失败');
        }
        return $this->success();
    }
    public function edit(){
        $shop_id=$this->request->shopId;
        $id = $this->request->param('id',0);
        $name = $this->request->param('name',null);
        $address=$this->request->param('address',null);
        $contacts_name=$this->request->param('contacts_name',null);
        $contacts_tel=$this->request->param('contacts_tel',null);
        $website=$this->request->param('website',null);
        $images=$this->request->param('images',null);
        $is_effect=$this->request->param('is_effect',null);
        if($id==0){
            return $this->error('id错误');
        }
        $SupplierModel= new SupplierModel();
        $all_info=$SupplierModel->where(['id'=>$id])->find();
        if(empty($all_info)) return $this->error('数据不存在');
        $s=[];
        $s[]=['name','=',$name];
        $s[]=['shop_id','=',$shop_id];
        $s[]=['id','<>',$id];
        $s[]=['is_delete','=',0];
        $info=$SupplierModel->where($s)->find();
        if(!empty($info)) return $this->error('数据重复');
        $up=[];
        if($name!=null) $up['name']=$name;
        if($address!=null) $up['address']=$address;
        if($contacts_name!=null) $up['contacts_name']=$contacts_name;
        if($contacts_tel!=null) $up['contacts_tel']=$contacts_tel;
        if($website!=null) $up['website']=$website;
        if($images!=null) $up['images']=json_encode($images);
        if($is_effect!=null) $up['is_effect']=$is_effect;
        $where['id']=$id;
        $res_data=SupplierModel::up($up,$where);
        return $this->success($res_data);
    }
    public function add(){
        $shop_id=$this->request->shopId;
        $name = $this->request->param('name',null);
        $address=$this->request->param('address',null);
        $contacts_name=$this->request->param('contacts_name',null);
        $contacts_tel=$this->request->param('contacts_tel',null);
        $website=$this->request->param('website',null);
        $images=$this->request->param('images',null);
        $is_effect=$this->request->param('is_effect',null);
        $SupplierModell= new SupplierModel();
        $s=[];
        $s[]=['name','=',$name];
        $s[]=['shop_id','=',$shop_id];
        $s[]=['is_delete','=',0];
        $all_info=$SupplierModell->where($s)->find();
        if(!empty($all_info)) return $this->error('数据重复');
        $images=json_encode($images);
        $insert=compact('shop_id','name','address','contacts_name','contacts_tel','website','images');
        if($is_effect!==null) $insert['is_effect']=$is_effect;
        $insert['add_time']=date('Y-m-d H:i:s');
        $insert['edit_time']=date('Y-m-d H:i:s');
        $insert_id=SupplierModel::insertGetId($insert);
        return $this->success($insert_id);
    }
    public function goods_add(){
        $shop_id=$this->request->shopId;
        $supplier_id = $this->request->param('supplier_id',null);
        $goods_ids=$this->request->param('goods_ids','');
        $SupplierGoodsModel= new SupplierGoodsModel();
        $goods_ids=explode(',',trim($goods_ids,','));
        $up=[];
        $up['is_delete']=1;
        SupplierGoodsModel::up($up,['supplier_id'=>$supplier_id]);
        foreach ($goods_ids as $goods_id){
            $insert=[];
            $insert['supplier_id']=$supplier_id;
            $insert['goods_id']=$goods_id;
            $insert['shop_id']=$shop_id;
            $insert['is_delete']=0;
            $s=[];
//            $s[]=['supplier_id','=',$supplier_id];
            $s[]=['goods_id','=',$goods_id];
            $all_info=$SupplierGoodsModel->where($s)->find();
            if(empty($all_info)){
                $insert['add_time']=date('Y-m-d H:i:s');
                SupplierGoodsModel::insertGetId($insert);
            }else{
                $insert['edit_time']=date('Y-m-d H:i:s');
                SupplierGoodsModel::up($insert,['id'=>$all_info['id']]);
            }
        }
        return $this->success();
    }
    public function goods_del(){
        $id = $this->request->param('id');
        if(empty($id)){
            return $this->error('ID错误');
        }
        $is_delete=1;
        $where['id']=$id;
        $info=SupplierGoodsModel::where($where)->find();
        if(empty($info)){
            return $this->error('数据不存在');
        }
        $up['is_delete']=$is_delete;
        $res=SupplierGoodsModel::up($up,$where);
        if(!$res){
            return $this->error('删除失败');
        }
        return $this->success();
    }

}

